package com.iotechn.unimall.admin.api.settingsPlatform;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.SettingsPlatformDO;
import com.iotechn.unimall.data.mapper.SettingsPlatformMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.List;


@Service
public class AdminSettingsPlatformServiceImpl implements AdminSettingsPlatformService {

    private final static String PLATFORM = "CA_PLATFORM";

    @Autowired
    private SettingsPlatformMapper settingsPlatformMapper;
    @Autowired
    private CacheComponent cacheComponent;

    @Override
    public Page<SettingsPlatformDO> list(Integer page, Integer limit, String value, Long adminId) throws ServiceException {
        Wrapper<SettingsPlatformDO> wrapper = new EntityWrapper<SettingsPlatformDO>();
        if (!StringUtils.isEmpty(value))
            wrapper.like("_value", value);
        wrapper.orderBy("id", false);
        Integer count = settingsPlatformMapper.selectCount(wrapper);
        List<SettingsPlatformDO> SettingsPlatformDOS = settingsPlatformMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(SettingsPlatformDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SettingsPlatformDO create(SettingsPlatformDO settingsPlatformDO, Long adminId) throws ServiceException {
        // 参数名唯一
        if(cacheComponent.hexists(PLATFORM,settingsPlatformDO.getKey()))
            throw new AdminServiceException(ExceptionDefinition.PLATFORM_EXIST);
        if (settingsPlatformMapper.insert(settingsPlatformDO) > 0) {
            settingsPlatformDO.setId(settingsPlatformDO.getId());
            cacheComponent.putHashRaw(PLATFORM,settingsPlatformDO.getKey(),settingsPlatformDO.getValue());
            if(settingsPlatformDO.getKey().equals("company_location") && settingsPlatformDO.getRemark()!=null){
                cacheComponent.putHashRaw(PLATFORM,settingsPlatformDO.getKey()+"_remark",settingsPlatformDO.getRemark());
            }
            return settingsPlatformDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(SettingsPlatformDO settingsPlatformDO, Long adminId) throws ServiceException {
        // 参数名唯一
//        if(cacheComponent.hexists(PLATFORM,settingsPlatformDO.getKey()))
//            throw new AdminServiceException(ExceptionDefinition.PLATFORM_EXIST);
        Long id = settingsPlatformDO.getId();
        if (id == null) return "fail";
        if (settingsPlatformMapper.updateById(settingsPlatformDO) > 0) {
            cacheComponent.putHashRaw(PLATFORM,settingsPlatformDO.getKey(),settingsPlatformDO.getValue());
            if(settingsPlatformDO.getKey().equals("company_location") && settingsPlatformDO.getRemark()!=null){
                cacheComponent.putHashRaw(PLATFORM,settingsPlatformDO.getKey()+"_remark",settingsPlatformDO.getRemark());
            }
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        SettingsPlatformDO platformDO = settingsPlatformMapper.selectById(id);
        if (settingsPlatformMapper.deleteById(id) > 0) {
            cacheComponent.delHashObj(PLATFORM,platformDO.getKey());
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public SettingsPlatformDO detail(Long id, Long adminId) throws ServiceException {
        return settingsPlatformMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.PLATFORM_NOT_EXIST);
        List<SettingsPlatformDO> list = settingsPlatformMapper.selectBatchIds(ids);
        if (settingsPlatformMapper.delete(new EntityWrapper<SettingsPlatformDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.PLATFORM_NOT_EXIST);
        list.forEach(i->cacheComponent.delHashObj(PLATFORM,i.getKey()));
        return "ok";
    }

    @PostConstruct
    public void init(){
        // 清除原始内容
        cacheComponent.del(PLATFORM);
        List<SettingsPlatformDO> list = settingsPlatformMapper.selectList(new EntityWrapper<>());
        list.forEach(i -> cacheComponent.putHashRaw(PLATFORM,i.getKey(),i.getValue()));
    }

}
